//
//  M1FiltrateVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/17.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1FiltrateVC.h"
#import "M1RangeSlider.h"
#import "M1CLLocationManage.h"

@interface M1FiltrateVC ()

@property (nonatomic, strong) UILabel *m1_location;

@property (nonatomic, strong) UILabel *m1_age;

@property (nonatomic, strong) UILabel *m1_bigAge;

@property (nonatomic, strong) M1RangeSlider *m1_slider;

@property (nonatomic, strong) UILabel *m1_locationLB;

@property (nonatomic, strong) UILabel *m1_constellationLB;

@property (nonatomic, strong) UILabel *m1_educationLB;

@property (nonatomic, strong) UIButton *m1_girls;

@property (nonatomic, strong) UIButton *m1_boys;

@property (nonatomic, strong) UIButton *m1_unlimited;

@end

@implementation M1FiltrateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"筛选";
    
    [self m1_createRightBarButtonItem:@"保存" titleColor:m1_color(0x000000) action:@selector(m1_rightAction)];
    
    [self m1_createUI];
    
    [self m1_createCityPositioning];
}

- (void)m1_rightAction{
    NSMutableDictionary *m1_dic = [NSMutableDictionary dictionaryWithCapacity:0];
    
    
    if (![self.m1_locationLB.text isEqualToString:@"不限"]) {
        m1_dic[@"abode"]= self.m1_locationLB.text;
    }else{
        if (self.m1_location.text.length >0&&![self.m1_location.text isEqualToString:@"定位失败"]) {
            m1_dic[@"abode"]= self.m1_location.text;
        }
    }
    
    m1_dic[@"age"] = [NSString stringWithFormat:@"%@%@", self.m1_age.text, self.m1_bigAge.text];
    
    
    if (![_m1_constellationLB.text isEqualToString:@"不限"]&&[M1User user].status.member_fees_status) {
        m1_dic[@"constellation"] = _m1_constellationLB.text;
    }
    if (![_m1_educationLB.text isEqualToString:@"不限"]&&[M1User user].status.member_fees_status) {
        m1_dic[@"education"] = _m1_educationLB.text;
    }
    if (self.m1_requestBlock) {
        self.m1_requestBlock(m1_dic);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) m1_locationAction:(UITapGestureRecognizer *)tap{
    WEAKSELF;
    if (![M1User user].status.member_fees_status) {
        [M1Alert m1_alertWithTitle:nil message:@"成为加V会员可食用高级筛选" buttonTitles:@[@"取消",@"前往"] action:^(NSInteger index) {
            if (index == 1) {
                //会员
                M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
                m1_vipVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:m1_vipVC animated:YES];
            }
        }];
        return;
    }
    [BRAddressPickerView showAddressPickerWithShowType:BRAddressPickerModeCity defaultSelected:nil isAutoSelect:NO themeColor:nil resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
        if ([province.name isEqualToString:@"香港"] || [province.name isEqualToString:@"澳门"]) {
            weakSelf.m1_locationLB.text = [NSString stringWithFormat:@"%@",province.name];
        }else{
            weakSelf.m1_locationLB.text = [NSString stringWithFormat:@"%@-%@",province.name,city.name];
        }
    } cancelBlock:^{
        
    }];
}
-(void)m1_constellationAction{
    WEAKSELF;
    if (![M1User user].status.member_fees_status) {
        [M1Alert m1_alertWithTitle:nil message:@"成为加V会员可食用高级筛选" buttonTitles:@[@"取消",@"前往"] action:^(NSInteger index) {
            if (index == 1) {
                //会员
                M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
                m1_vipVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:m1_vipVC animated:YES];
            }
        }];
        return;
    }
    
    [BRStringPickerView showStringPickerWithTitle:@"星座" dataSource:@[@"不限",@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"狮子座",@"处女座",@"天秤座",@"天蝎座",@"射手座",@"摩羯座",@"水瓶座",@"双鱼座"] defaultSelValue:_m1_constellationLB.text resultBlock:^(id selectValue) {
        weakSelf.m1_constellationLB.text = selectValue;
    }];
}

-(void)m1_education{
    WEAKSELF;
    if (![M1User user].status.member_fees_status) {
        [M1Alert m1_alertWithTitle:nil message:@"成为加V会员可食用高级筛选" buttonTitles:@[@"取消",@"前往"] action:^(NSInteger index) {
            if (index == 1) {
                //会员
                M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
                m1_vipVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:m1_vipVC animated:YES];
            }
        }];
        return;
    }
    [BRStringPickerView showStringPickerWithTitle:@"学历" dataSource:@[@"不限",@"本科以下",@"本科",@"研究生",@"博士及以上"] defaultSelValue:_m1_educationLB.text resultBlock:^(id selectValue) {
        weakSelf.m1_educationLB.text = selectValue;
    }];
}



-(void)m1_sliderValueChanged:(id)paramSender{
    self.m1_age.text = [NSString stringWithFormat:@"%.f", _m1_slider.m1_lowerValue];
    self.m1_bigAge.text = [NSString stringWithFormat:@"-%.f", _m1_slider.m1_upperValue];
}


- (void) m1_createCityPositioning{
    __weak typeof(self)weakSelf = self;
    [[M1CLLocationManage shareInstance] m1_getLocation:^(NSString *province, NSString *city) {
        weakSelf.m1_location.text = [NSString stringWithFormat:@"%@-%@", province, city];
    } faild:^(NSString *error) {
        weakSelf.m1_location.text = @"定位失败";
    }];
}

- (void)m1_sexAction:(UIButton *)sender{
    if (sender.tag == 1) {
        [self m1_changeBtn:self.m1_girls bgImage:@"filtrate_view" titleColor:m1_color(0xFFFFFF)];
        
        [self m1_changeBtn:self.m1_boys bgImage:@"" titleColor:m1_color(0x333333)];
        
        [self m1_changeBtn:self.m1_unlimited bgImage:@"" titleColor:m1_color(0x333333)];
    }else if (sender.tag == 2){
        [self m1_changeBtn:self.m1_boys bgImage:@"filtrate_view" titleColor:m1_color(0xFFFFFF)];
        
        [self m1_changeBtn:self.m1_girls bgImage:@"" titleColor:m1_color(0x333333)];
        
        [self m1_changeBtn:self.m1_unlimited bgImage:@"" titleColor:m1_color(0x333333)];
    }else{
        [self m1_changeBtn:self.m1_unlimited bgImage:@"filtrate_view" titleColor:m1_color(0xFFFFFF)];
        
        [self m1_changeBtn:self.m1_boys bgImage:@"" titleColor:m1_color(0x333333)];
        
        [self m1_changeBtn:self.m1_girls bgImage:@"" titleColor:m1_color(0x333333)];
    }
}

- (void) m1_createUI{
    UILabel *label = [[UILabel alloc] init];
    label.text = @"向我展示";
    label.textColor = m1_color(0x999999);
    label.font = m1_font(15);
    [self.view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(18);
        make.top.offset(m1_NAV_HEIGHT + 15);
    }];
    
    UIView *locationView = [[UIView alloc] init];
    locationView.backgroundColor = m1_color(0xFFFFFF);
    [self.view addSubview:locationView];
    
    [locationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(label.mas_bottom).offset(15);
        make.height.offset(54);
    }];
    
    UILabel *title = [[UILabel alloc] init];
    title.text = @"我的漫游位置";
    title.textColor = m1_color(0x333333);
    title.font = m1_font(15);
    [locationView addSubview:title];
    
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(locationView.mas_centerY);
        make.left.offset(20);
    }];
    

    
    self.m1_location = [[UILabel alloc] init];
    _m1_location.text = @"获取中...";
    _m1_location.textColor = m1_color(0xFFF543C);
    _m1_location.font = m1_font(15);
    [locationView addSubview:_m1_location];
    
    [_m1_location mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(locationView.mas_centerY);
        make.right.offset(-21);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = m1_color(0xCCCCCC);
    [locationView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(0.5);
    }];
    
    
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = m1_color(0xFFFFFF);
    [self.view addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(locationView.mas_bottom).offset(0);
        make.height.offset(300);
    }];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"年龄";
    label1.textColor = m1_color(0x999999);
    label1.font = m1_font(15);
    [bgView addSubview:label1];
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(20);
    }];
    
    self.m1_age = [[UILabel alloc] init];
    _m1_age.text = @"18";
    _m1_age.textColor = m1_color(0xFFF543C);
    _m1_age.font = m1_font(15);
    [bgView addSubview:_m1_age];
    
    [_m1_age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label1.mas_centerY);
        make.left.equalTo(label1.mas_right).offset(10);
    }];
    
    self.m1_bigAge = [[UILabel alloc] init];
    _m1_bigAge.text = @"-26";
    _m1_bigAge.textColor = m1_color(0xFFF543C);
    _m1_bigAge.font = m1_font(15);
    [bgView addSubview:_m1_bigAge];
    
    [_m1_bigAge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label1.mas_centerY);
        make.left.equalTo(self.m1_age.mas_right).offset(0);
    }];
    
    _m1_slider = [[M1RangeSlider alloc] initWithFrame:CGRectMake(40, 58, SCREENWIDTH - 40 * 2, 22)];
    [_m1_slider addTarget:self action:@selector(m1_sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [bgView addSubview:_m1_slider];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"高级筛选";
    label2.textColor = m1_color(0x333333);
    label2.font = m1_font(16);
    [bgView addSubview:label2];
    
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.equalTo(self.m1_slider.mas_bottom).offset(20);
    }];
    
    UILabel *m1_LB1 = [[UILabel alloc] init];
    m1_LB1.text = @"(仅加V会员可用)";
    m1_LB1.textColor = m1_color(0xFFF543C);
    m1_LB1.font = m1_font(13);
    [bgView addSubview:m1_LB1];
    
    [m1_LB1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label2.mas_right).offset(10);
        make.centerY.equalTo(label2.mas_centerY);
    }];
    
    
    UIView *abode = [[UIView alloc] init];
    abode.backgroundColor = m1_color(0xFFFFFF);
    [self.view addSubview:abode];
    
    [abode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(m1_LB1.mas_bottom).offset(25);
        make.height.offset(54);
    }];
    
    UILabel *title3 = [[UILabel alloc] init];
    title3.text = @"所在地";
    title3.textColor = m1_color(0x999999);
    title3.font = m1_font(15);
    [abode addSubview:title3];
    
    [title3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(abode.mas_centerY);
        make.left.offset(20);
    }];
    
    UIImageView *across3 = [[UIImageView alloc] init];
    across3.image = LOAD_LOCAL_IMG(@"personal_icon_across");
    [abode addSubview:across3];
    
    [across3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(abode.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(5, 9));
    }];
    
    self.m1_locationLB = [[UILabel alloc] init];
    _m1_locationLB.text = @"不限";
    _m1_locationLB.textColor = m1_color(0x333333);
    _m1_locationLB.font = m1_font(15);
    [abode addSubview:_m1_locationLB];
    
    [_m1_locationLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(abode.mas_centerY);
        make.right.equalTo(across3.mas_left).offset(-21);
    }];
    
    UIView *line3 = [[UIView alloc] init];
    line3.backgroundColor = m1_color(0xCCCCCC);
    [abode addSubview:line3];
    
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(0.5);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(m1_locationAction:)];
    [abode addGestureRecognizer:tap];
    
    UIView *constellationView = [[UIView alloc] init];
    constellationView.backgroundColor = m1_color(0xFFFFFF);
    [self.view addSubview:constellationView];
    
    [constellationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(abode.mas_bottom).offset(0);
        make.height.offset(54);
    }];
    
    UILabel *title1 = [[UILabel alloc] init];
    title1.text = @"星座";
    title1.textColor = m1_color(0x999999);
    title1.font = m1_font(15);
    [constellationView addSubview:title1];
    
    [title1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(constellationView.mas_centerY);
        make.left.offset(20);
    }];
    
    UIImageView *across1 = [[UIImageView alloc] init];
    across1.image = LOAD_LOCAL_IMG(@"personal_icon_across");
    [constellationView addSubview:across1];
    
    [across1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(constellationView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(5, 9));
    }];
    
    self.m1_constellationLB = [[UILabel alloc] init];
    _m1_constellationLB.text = @"不限";
    _m1_constellationLB.textColor = m1_color(0x333333);
    _m1_constellationLB.font = m1_font(15);
    [constellationView addSubview:_m1_constellationLB];
    
    [_m1_constellationLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(constellationView.mas_centerY);
        make.right.equalTo(across1.mas_left).offset(-21);
    }];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = m1_color(0xCCCCCC);
    [constellationView addSubview:line1];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(0.5);
    }];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(m1_constellationAction)];
    [constellationView addGestureRecognizer:tap1];
    
    
    UIView *educationView = [[UIView alloc] init];
    educationView.backgroundColor = m1_color(0xFFFFFF);
    [self.view addSubview:educationView];
    
    [educationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(constellationView.mas_bottom).offset(0);
        make.height.offset(54);
    }];
    
    UILabel *title2 = [[UILabel alloc] init];
    title2.text = @"学历";
    title2.textColor = m1_color(0x999999);
    title2.font = m1_font(15);
    [educationView addSubview:title2];
    
    [title2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(educationView.mas_centerY);
        make.left.offset(20);
    }];
    
    UIImageView *across2 = [[UIImageView alloc] init];
    across2.image = LOAD_LOCAL_IMG(@"personal_icon_across");
    [educationView addSubview:across2];
    
    [across2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.centerY.equalTo(educationView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(5, 9));
    }];
    
    self.m1_educationLB = [[UILabel alloc] init];
    _m1_educationLB.text = @"不限";
    _m1_educationLB.textColor = m1_color(0x333333);
    _m1_educationLB.font = m1_font(15);
    [educationView addSubview:_m1_educationLB];
    
    [_m1_educationLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(educationView.mas_centerY);
        make.right.equalTo(across2.mas_left).offset(-21);
    }];
    
    UIView *line2 = [[UIView alloc] init];
    line2.backgroundColor = m1_color(0xCCCCCC);
    [educationView addSubview:line2];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(0.5);
    }];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(m1_education)];
    [educationView addGestureRecognizer:tap2];
    
}

- (UIButton *) createBtn:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)color bgImage:(NSString *)image tag:(NSInteger)tag{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.backgroundColor = m1_color(0xEFF1F3);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = m1_font(15);
    [button setBackgroundImage:LOAD_LOCAL_IMG(image) forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.tag = tag;
    [button addTarget:self action:@selector(m1_sexAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 40 / 2;
    button.layer.masksToBounds = YES;
    return button;
}

- (void)m1_changeBtn:(UIButton *)button bgImage:(NSString *)image titleColor:(UIColor *)color{
    [button setBackgroundImage:LOAD_LOCAL_IMG(image) forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
}

- (void) m1_createRightBarButtonItem:(NSString *)title titleColor:(UIColor *)color action:(SEL)action{
    CGFloat width;
    if ([title length] > 2) {
        width = 70;
    }else{
        width = 40;
    }
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, width, 44);
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = m1_font(16);
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
